package jp.co.hcs.attendancemanagement.dialog.logic;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import jp.co.hcs.attendancemanagement.data.dao.ProjectDAO;
import jp.co.hcs.attendancemanagement.data.entity.Project;
import jp.co.hcs.attendancemanagement.dialog.logic.dto.ProjectDTO;
import jp.co.hcs.attendancemanagement.logic.LogicBase;

/**
 * The Class ProjectSearchLogic.
 */
public class ProjectSearchLogic extends LogicBase {

  /** The project dao. */
  private ProjectDAO projectDao;

  /**
   * Search project.
   * 
   * @param departmentId the department id
   * @return the list
   */
  public List<ProjectDTO> searchProject(String departmentId) {
    List<ProjectDTO> projectList = new ArrayList<ProjectDTO>();
    try {
      List<Project> result = getProjectDao().retrieveProjectListByDepartment(departmentId);
      ProjectDTO dto;
      for (Project prj : result) {
        dto = new ProjectDTO();
        String projectId = prj.getProjectId();
        dto.setProjectId(projectId);
        dto.setProjectName(prj.getProjectName());
        // 1-6 is project code and 7-8 is seri from project number
        dto.setProjectCode(projectId.substring(0, 6));
        dto.setSeri(projectId.substring(6));
        projectList.add(dto);
      }
    } catch (SQLException e) {
    }
    return projectList;
  }

  /**
   * Gets the project dao.
   * 
   * @return the project dao
   */
  public ProjectDAO getProjectDao() {
    if (projectDao == null) {
      projectDao = daoFactory.getProjectDAO();
    }

    return projectDao;
  }

  /**
   * Sets the project dao.
   * 
   * @param projectDao the new project dao
   */
  public void setProjectDao(ProjectDAO projectDao) {
    this.projectDao = projectDao;
  }

}
